package
{
	import com.greensock.TweenLite;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.ui.Keyboard;
	import flash.utils.Timer;
	
	[SWF(frameRate="50")]
	public class TimeOut extends Sprite
	{
		[Embed(source="1.png")]
		private var _imageClass1:Class;
		[Embed(source="2.png")]
		private var _imageClass2:Class;
		[Embed(source="3.png")]
		private var _imageClass3:Class;
		
		private var _container:Sprite;
		private var _bitmap:Bitmap;
		private var _timer:Timer;
		private var _vecBitmapData:Vector.<BitmapData>;
		
		public function TimeOut()
		{
			super();
			init();
		}
		
		private function init():void
		{
			_vecBitmapData = new Vector.<BitmapData>();
			_vecBitmapData.push( (new _imageClass1() as Bitmap).bitmapData );
			_vecBitmapData.push( (new _imageClass2() as Bitmap).bitmapData );
			_vecBitmapData.push( (new _imageClass3() as Bitmap).bitmapData );
			
			_container = new Sprite();
			_container.x = 200;
			_container.y = 200;
			addChild( _container );
			_bitmap = new Bitmap();
			_container.addChild( _bitmap );

			_timer = new Timer(1000, 0);
			_timer.addEventListener( TimerEvent.TIMER, onTimer );
			
			stage.addEventListener( KeyboardEvent.KEY_DOWN, onKeydown );
		}
		
		private function onKeydown( e:KeyboardEvent ):void
		{
			if( e.keyCode==Keyboard.SPACE )
			{
				start();
			}
		}
		
		private var _currentIndex:int = 0;
		private function start():void
		{
			_currentIndex = 2;
			_timer.reset();
			_timer.start();
			next();
		}
		
		private function onTimer( e:TimerEvent ):void
		{
			next();
		}
		
		private function next():void
		{
			if( _currentIndex >= 0 )
			{
				_bitmap.scaleX = 0;
				_bitmap.alpha = 1;
				_bitmap.scaleY = 0;
				_bitmap.bitmapData = _vecBitmapData[_currentIndex];
				_currentIndex--;
				TweenLite.to( _bitmap, 0.1, {scaleX:1, scaleY:1, onComplete:onComplete} );
			}
			else
			{
				_bitmap.bitmapData = null;
				_timer.stop();
				trace( "sdfsdfsdf" );
			}
		}
		
		private function onComplete():void
		{
			TweenLite.to( _bitmap, 0.4, {alpha:0,delay:0.4} );
		}
	}
}















